<template>
  <div class="add-student-container">
    <el-card class="add-student-card">
      <template #header>
        <div class="card-header">
          <h2>添加学生</h2>
          <el-button type="primary" @click="goBack">
            <el-icon><ArrowLeft /></el-icon> 返回
          </el-button>
        </div>
      </template>

      <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          label-width="120px"
          label-position="top"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
              v-model="form.username"
              placeholder="请输入用户名"
              clearable
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入密码"
              show-password
              clearable
          />
        </el-form-item>

        <el-form-item label="真实姓名" prop="realName">
          <el-input
              v-model="form.realName"
              placeholder="请输入真实姓名"
              clearable
          />
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input
              v-model="form.email"
              placeholder="请输入邮箱"
              clearable
          />
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input
              v-model="form.phone"
              placeholder="请输入手机号"
              clearable
          />
        </el-form-item>

        <el-form-item label="头像链接" prop="avatar">
          <el-input
              v-model="form.avatar"
              placeholder="请输入头像链接"
              clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              @click="submitForm"
              :loading="loading"
          >
            添加学生
          </el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'
import { addStudent } from '@/api/teacher'

const router = useRouter()
const formRef = ref(null)

const form = reactive({
  username: '',
  password: '',
  realName: '',
  email: '',
  phone: '',
  avatar: ''
})

const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ]
})

const loading = ref(false)

const submitForm = async () => {
  try {
    await formRef.value.validate()
    loading.value = true

    const response = await addStudent({
      username: form.username,
      password: form.password,
      realName: form.realName,
      email: form.email || undefined,
      phone: form.phone || undefined,
      avatar: form.avatar || undefined
    })

    ElMessage.success('学生添加成功')
    router.push('/teacher/dashboard')
  } catch (error) {
    if (error.response) {
      const { status, data } = error.response
      switch (status) {
        case 400:
          ElMessage.error(data.message || '请求参数错误')
          break
        case 409:
          ElMessage.error(data.message || '用户名已存在')
          break
        case 403:
          ElMessage.error('没有权限添加学生')
          break
        default:
          ElMessage.error('添加学生失败')
      }
    } else {
      ElMessage.error('网络错误，请稍后再试')
    }
  } finally {
    loading.value = false
  }
}

const resetForm = () => {
  formRef.value.resetFields()
}

const goBack = () => {
  router.go(-1)
}
</script>

<style scoped>
.add-student-container {
  padding: 20px;
  display: flex;
  justify-content: center;
}

.add-student-card {
  width: 100%;
  max-width: 800px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-form {
  padding: 20px;
}

.el-form-item {
  margin-bottom: 22px;
}

.el-input {
  width: 100%;
}
</style>